package com.kangye.exam.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.kangye.exam.bean.Teacher;
import com.kangye.exam.service.TeacherService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/teacher")
@CrossOrigin
public class TeacherController {

    @Resource
    private TeacherService teacherService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 登陆
     *
     * @param teacherUser 账号
     * @param teacherPass 密码
     * @return 实例对象
     */
    @RequestMapping("/teacherLogin")
    public Object TeacherLogin(String teacherUser, String teacherPass){
        //map集合用于前台传值
        Map<String,Object> map = new HashMap();
        //redis缓存存放的数据集合
        List<Teacher> tea = new ArrayList<>();
        //System.out.println(teacherUser+" "+teacherPass);
        //从redis缓存中查询登陆的所有用户
        tea = redisTemplate.opsForList().range("teacher",0,-1);
        //判断redis缓存中是否有用户登陆
        if (tea.size()>0){
            //redis缓存中有登陆的用户信息
            //循环取出所有的用户信息
            for (int i = 0 ; i < tea.size() ; i++ ){
                //把json格式的用户转成User对象类型
                Teacher t = JSON.parseObject(String.valueOf(tea.get(i)),Teacher.class);
                //判断集合中是否有当前登陆的用户
                if ( t.getTeacherUser().equals(teacherUser) && t.getTeacherPass().equals(teacherPass) ){
                    //存入map集合
                    map.put("code",0);
                    t.setTeacherPass("");
                    map.put("data",t);
                    return map;
                }
            }
        }
        //从数据库中查找用户
        Teacher t = teacherService.getTeaLogin(teacherUser,teacherPass);
        if (t != null){
            //把当前用户存放到redis中
            redisTemplate.opsForList().leftPush("teacher",JSON.toJSONString(t));
            //设置用户过期时间3600s
            redisTemplate.expire("teacher",3600,TimeUnit.SECONDS);
            //存入map集合
            t.setTeacherPass("");
            map.put("code",0);
            map.put("teacherUser",t);
        }else {
            map.put("code",-1);
        }
        return map;
    }

    /**
     * 查询所有数据
     *
     * @param pageNum   当前页
     * @param pageSize  每页条数
     * @return 对象列表
     */
    @RequestMapping("/teaAll")
    public Object teaAll(Integer pageNum, Integer pageSize){
        PageInfo info = teacherService.queryTAll(pageNum,pageSize);
        System.out.println(info.getPages());
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("count", info.getTotal());
        map.put("limit", info.getPageSize());
        map.put("data", info.getList());
        return map;
    }

    /**
     * 条件查询->通过关键字(用户名)查询
     *
     * @param pageNum 当前页
     * @param pageSize 每页条数
     * @param name 关键字
     * @return 对象列表
     */
    @RequestMapping("/teaList")
    public Object teaList(String name,Integer pageNum,Integer pageSize){
        System.out.println(name);
        Map<String,Object> map = new HashMap<>();
        PageInfo info = null;
        if (name != null || name == ""){
            info = teacherService.teacherList(name,pageNum,pageSize);
        }else {
            info = teacherService.queryTAll(pageNum,pageSize);
        }
        if (info.getList()!=null){
            map.put("code", 0);
            map.put("count", info.getTotal());
            map.put("limit", info.getPageSize());
            map.put("data", info.getList());
        }else {
            map.put("code",-1);
        }

        return map;
    }

    /**
     * 新增数据
     *
     * @param teacher 实例对象
     */
    @RequestMapping("/addTea")
    public Object addTea(Teacher teacher){
        System.out.println(teacher);
        Date date = new Date();//创建日期对象
        //获取当前时间  格式:2021-01-20 13:33:07
        String sd = String.format("%tF", date) + " " + String.format("%tT", date);
        teacher.setTeacherCreate(sd);
        teacherService.insertTea(teacher);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

    /**
     * 通过主键查询数据
     *
     * @param id 主键
     */
    @RequestMapping("/queTeaId")
    public Object queryTeaId(int id){
        Map<String,Object> map = new HashMap<>();
        Teacher t = teacherService.queryTeaId(id);
        map.put("data",t);
        return map;
    }

    /**
     * 修改数据
     *
     * @param teacher 实例对象
     */
    @RequestMapping("/updateTea")
    public Object updateTea(Teacher teacher){
        teacherService.updateTea(teacher);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @RequestMapping("/deleteTId")
    public Object deleteTId(int id){
        teacherService.deleteTId(id);
        Map<String,Object> map = new HashMap<>();
        map.put("code", 0);
        return map;
    }

}
